﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using QRCoder;

public partial class Admin_DevMan_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        string pronum = FProNum.Text.Trim();
        string proname = FProName.Text.Trim();
        //图片
        string IFileName = pronum;
        string IFileFoldName = "/Photos/";
        string IFilePath = IFileFoldName + IFileName;
        bool IfileIsValid = false;
        string proimg = "";
        //如果确认了文件上传，则判断文件类型是否符合要求
        if (this.FileUploadImg.HasFile)
        {
            //获取上传文件的后缀名
            String fileExtension = System.IO.Path.GetExtension(this.FileUploadImg.FileName).ToLower();//ToLower是将Unicode字符的值转换成它的小写等效项
                                                                                                      //定义一个数组，把文件后缀名的的类型总结出来
            String[] restrictExtension = { ".jpg", ".bmp", ".png","jpeg" };
            //判断文件类型是否符合要求
            for (int i = 0; i < restrictExtension.Length; i++)
            {
                if (fileExtension == restrictExtension[i])
                {
                    IfileIsValid = true;
                    IFileName += restrictExtension[i];
                }
            }
        }
        IFilePath = IFileFoldName + IFileName;
        string IFileFullPath = Server.MapPath(IFilePath);
        //如果文件类型符合要求，则用SaveAs方法实现上传，并显示信息
        if (IfileIsValid == true)
        {
            try
            {
                FileUploadImg.SaveAs(IFileFullPath);
                proimg =IFileName;

            }
            catch
            {
                FileUploadImg_Label.Text = "图片格式不正确，请重新上传";
            }
        }
         
        string danwei = FDanWei.Text.Trim();
        string manuname = FManuName.Text.Trim();
        string manuinfo = FManuInfo.Text.Trim();
        string date = FDate.Text.Trim(); ;
        string proplace = FProPlace.Text.Trim();
        string dutyname = FDutyName.Text.Trim();
        string prozt = RBLZT.SelectedValue;
        string proyy = RBLYY.SelectedValue;
        string proworth = FProWorth.Text.Trim();
        string category = FProCateg.SelectedValue.Trim();
        string function = FProFunction.Text.Trim();
        string remarks = FProRemarks.Text.Trim();
        //操作手册
        string OFileName = pronum;
        string OFileFoldName = "/Operation/";
        string OFilePath = OFileFoldName + OFileName;
        bool OfileIsValid = false;
        if (this.FileUploadUse.HasFile)
        {
            String fileExtension = System.IO.Path.GetExtension(this.FileUploadUse.FileName).ToLower();
            String[] restrictExtension = { ".pdf", ".doc", ".docx", ".jpg", ".png" };
            for (int i = 0; i < restrictExtension.Length; i++)
            {
                if (fileExtension == restrictExtension[i])
                {
                    OfileIsValid = true;
                    OFileName += restrictExtension[i];
                }
            }
        }
        string prooper = "";
        OFilePath = OFileFoldName + OFileName;
        string OFileFullPath = Server.MapPath(OFilePath);
        if (OfileIsValid == true)
        {
            try
            {
                FileUploadUse.SaveAs(OFileFullPath);
                prooper = OFileName;

            }
            catch
            {
                FileUploadUse_Label.Text = "不支持操作手册文件格式，请重新上传！";
            }
        }

        //string matter = FMatter.Text.Trim();
        //string remarks = FProRemarks.Text.Trim();


        //生成二维码
        string name = pronum;
        string strCode = "http://gpd.alanfung.cn/move_login.aspx?id=" + name;
        QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
        QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
        QRCode qrcode = new QRCode(qrCodeData);

        // qrcode.GetGraphic 方法可参考最下发“补充说明”
        Bitmap icon = new Bitmap(Server.MapPath("~/qrcode/icon_ultimate.jpg"));
        Bitmap qrCodeImage = qrcode.GetGraphic(15, Color.Gray, Color.GhostWhite, icon, 20, 6, true);
        MemoryStream ms = new MemoryStream();
        qrCodeImage.Save(ms, ImageFormat.Png);

        string filepath = Server.MapPath("~/qrcode/" + name + ".png");
        qrCodeImage.Save(filepath);
        // 如果想保存图片 可使用  qrCodeImage.Save(filePath);

        // 响应类型
        //context.Response.ContentType = "image/Jpeg";
        //输出字符流
        //context.Response.BinaryWrite(ms.ToArray());


        /* GetGraphic方法参数说明
                 public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon = null, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true)
             * 
                 int pixelsPerModule:生成二维码图片的像素大小 ，我这里设置的是5 
             * 
                 Color darkColor：暗色   一般设置为Color.Black 黑色
             * 
                 Color lightColor:亮色   一般设置为Color.White  白色
             * 
                 Bitmap icon :二维码 水印图标 例如：Bitmap icon = new Bitmap(context.Server.MapPath("~/images/zs.png")); 默认为NULL ，加上这个二维码中间会显示一个图标
             * 
                 int iconSizePercent： 水印图标的大小比例 ，可根据自己的喜好设置 
             * 
                 int iconBorderWidth： 水印图标的边框
             * 
                 bool drawQuietZones:静止区，位于二维码某一边的空白边界,用来阻止读者获取与正在浏览的二维码无关的信息 即是否绘画二维码的空白边框区域 默认为true
   */


        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;

        //try
        //{
        try
        {
            conn.Open();
            string SQL = "insert into Products (pro_num,pro_name,pro_img,pro_danwei,pro_manuname,pro_manuinfo,buydate,pro_location,auditor,condition,ordernot,pro_worth,pro_category,pro_function,operation_manual,remarks) values (@pro_num,@pro_name,@pro_img,@pro_danwei,@pro_manuname,@pro_manuinfo,@buydate,@pro_location,@auditor,@condition,@ordernot,@pro_worth,@pro_category,@pro_function,@operation_manual,@remarks)";

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = SQL;

            //依次添加每个字段的值
            SqlParameter ppro_num = new SqlParameter("@pro_num", SqlDbType.NVarChar, 10);
            ppro_num.Value = pronum;
            cmd.Parameters.Add(ppro_num);

            SqlParameter ppro_name = new SqlParameter("@pro_name", SqlDbType.NVarChar, 20);
            ppro_name.Value = proname;
            cmd.Parameters.Add(ppro_name);

            SqlParameter ppro_img = new SqlParameter("@pro_img", SqlDbType.NVarChar, 30);
            ppro_img.Value = proimg;
            cmd.Parameters.Add(ppro_img);

            SqlParameter ppro_danwei = new SqlParameter("@pro_danwei", SqlDbType.NVarChar, 20);
            ppro_danwei.Value = danwei;
            cmd.Parameters.Add(ppro_danwei);

            SqlParameter ppro_manuname = new SqlParameter("@pro_manuname", SqlDbType.NVarChar, 20);
            ppro_manuname.Value = manuname;
            cmd.Parameters.Add(ppro_manuname);

            SqlParameter pmanuinfo = new SqlParameter("@pro_manuinfo", SqlDbType.NVarChar, 40);
            pmanuinfo.Value = manuinfo;
            cmd.Parameters.Add(pmanuinfo);

            SqlParameter pbuydate = new SqlParameter("@buydate", SqlDbType.NVarChar, 20);
            pbuydate.Value = date;
            cmd.Parameters.Add(pbuydate);

            SqlParameter ppro_location = new SqlParameter("@pro_location", SqlDbType.NVarChar, 20);
            ppro_location.Value = proplace;
            cmd.Parameters.Add(ppro_location);

            SqlParameter pauditor = new SqlParameter("@auditor", SqlDbType.NVarChar, 10);
            pauditor.Value = dutyname;
            cmd.Parameters.Add(pauditor);

            SqlParameter pcondition = new SqlParameter("@condition", SqlDbType.NVarChar, 10);
            pcondition.Value = prozt;
            cmd.Parameters.Add(pcondition);

            SqlParameter pordernot = new SqlParameter("@ordernot", SqlDbType.NVarChar, 10);
            pordernot.Value = proyy;
            cmd.Parameters.Add(pordernot);

            SqlParameter ppro_worth = new SqlParameter("@pro_worth", SqlDbType.NVarChar, 10);
            ppro_worth.Value = proworth;
            cmd.Parameters.Add(ppro_worth);

            SqlParameter ppro_category = new SqlParameter("@pro_category", SqlDbType.NVarChar, 20);
            ppro_category.Value = category;
            cmd.Parameters.Add(ppro_category);

            SqlParameter ppro_function = new SqlParameter("@pro_function", SqlDbType.NVarChar, 100);
            ppro_function.Value = function;
            cmd.Parameters.Add(ppro_function);

            SqlParameter poper = new SqlParameter("@operation_manual", SqlDbType.NVarChar, 20);
            poper.Value = prooper;
            cmd.Parameters.Add(poper);

            SqlParameter premarks = new SqlParameter("@remarks", SqlDbType.NVarChar, 100);
            premarks.Value = remarks;
            cmd.Parameters.Add(premarks);

            int R = cmd.ExecuteNonQuery();  //R 的值就是返回影响表的行数
            if (R > 0)
            {
                Session["ReturnPath"] = "Admin_DevMan_Add.aspx";
                Session["Message"] = "恭喜，操作成功！";
                Response.Redirect("Success.aspx");
            }
            else
            {
                Session["Message"] = "插入失败，请检查您的操作！";
                Response.Redirect("Error.aspx");
            }
            //Session["ReturnPath"] = "Admin_Add.aspx";
            //Response.Redirect("Mid.aspx");
        }
        finally
        {
            conn.Close();
        }
    }
    //catch
    //{
    //    Response.Redirect("Error.aspx");
    //}
    //}
}
